package com.lucy.rabc.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.lucy.common.converter.EncryptConverter;
import com.lucy.common.converter.JpaColumnConverter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.hibernate.Hibernate;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
import java.util.Set;

/**
 * @author kindear
 */
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
@Table(name = "sys_user")
@ApiModel(value = "用户信息实体",description = "用户实体信息管理")
@EntityListeners(AuditingEntityListener.class)
public class SysUser implements Serializable {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @ApiModelProperty(value = "用户ID",name = "id")
    Long id;

    @ApiModelProperty("部门ID")
    @Column(name = "dept_id",nullable = false)
    private Long deptId;

    @ApiModelProperty("用户账号")
    @Column(name = "user_name",length = 64)
    private String userName;

    @ApiModelProperty("用户昵称")
    @Column(name = "nick_name",length = 64)
    private String nickName;

    @ApiModelProperty("用户手机号")
    @Column(name = "phone_number")
    String phoneNumber;

    /** 用户邮箱 & 用户登录账号 **/
    @ApiModelProperty("用户邮箱")
    @Column(name = "email")
    String email;

    /**
     * 岗位ID
     */
    @ApiModelProperty("岗位ID")
    @Column(name = "post_id")
    private Long postId;

    /**
     * 身份证
     */
    @ApiModelProperty("证件号码")
    @Column(name = "card_id")
    private String cardId;



    /**
     * 用户性别 可选 男 女 第三性别
     */
    @ApiModelProperty("用户性别")
    @Column(name = "sex")
    private String sex;

    /**
     * 用户头像地址 OSS
     */
    @ApiModelProperty("用户头像")
    @Column(name = "avatar",length = 512)
    private String avatar;

    /**
     * 账号状态 可选 0 正常！ 1 禁用
     */
    @ApiModelProperty("账号状态")
    @Column(name = "status",nullable = false)
    private Integer status;

    /** 用户登录密码 & 用户隐私数据 **/
    @JsonIgnore
    @Convert(converter = EncryptConverter.class)
    @ApiModelProperty("用户登录密码")
    @Column(name = "password")
    String password;

    /**
     * 用户昵称首字母
     */
    @ApiModelProperty("用户昵称首字母")
    @Column(name = "initial")
    private String initial;



    /**
     * 角色组
     */
    @JsonIgnoreProperties(value = "users")
    @ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch = FetchType.LAZY)
    @JoinTable(name = "sys_user_role", joinColumns = {
            @JoinColumn(name = "user_id", referencedColumnName = "id")}, inverseJoinColumns = {
            @JoinColumn(name = "role_id", referencedColumnName = "id")})
    @ToString.Exclude
    Set<SysRole> roles;


    /**
     * 拓展请求参数
     */

    @ApiModelProperty("拓展参数")
    @Column(name = "ext_params",length = 2048)
    String extParams;


    @CreatedDate
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @ApiModelProperty(value = "记录创建时间",name = "createdTime")
    @Column(name = "created_time")
    private Date createdTime;
    @LastModifiedDate
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @ApiModelProperty(value = "记录更新时间",name = "updatedTime")
    @Column(name = "updated_time")
    private Date updatedTime;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) {
            return false;
        }
        SysUser user = (SysUser) o;
        return id != null && Objects.equals(id, user.id);
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}
